local sk__huaibi = fk.CreateSkill {

  name = "sk__huaibi",

  tags = { Skill.Compulsory, },

}



sk__huaibi:addEffect(fk.GameStart, {
  name = "sk__huaibi",
  anim_type = "defensive",
  derived_piles = "sk__caojie_xi",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__huaibi.name) then
      return #player:getPile("sk__caojie_xi") == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, sk__huaibi.name)
    if player.dead or player:isKongcheng() then return end
    local card = room:askForCard(player, 1, 1, false, sk__huaibi.name, false, ".", "#sk__huaibi-card")
    player:addToPile("sk__caojie_xi", card, true, sk__huaibi.name, player.id)
  end,
})
sk__huaibi:addEffect(fk.EventPhaseStart, {
  name = "sk__huaibi",
  anim_type = "defensive",
  derived_piles = "sk__caojie_xi",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__huaibi.name) then
      return target == player and player.phase == Player.Finish and #player:getPile("sk__caojie_xi") == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, sk__huaibi.name)
    if player.dead or player:isKongcheng() then return end
    local card = room:askForCard(player, 1, 1, false, sk__huaibi.name, false, ".", "#sk__huaibi-card")
    player:addToPile("sk__caojie_xi", card, true, sk__huaibi.name, player.id)
  end,
})
sk__huaibi:addEffect(fk.PreCardEffect, {
  name = "sk__huaibi",
  anim_type = "defensive",
  derived_piles = "sk__caojie_xi",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__huaibi.name) then
      return #player:getPile("sk__caojie_xi") > 0 and data.from ~= player and data.to == player and
          (data.card.trueName == "slash" or data.card:isCommonTrick()) and
          data.card.suit ~= Card.NoSuit and data.card.suit == Fk:getCardById(player:getPile("sk__caojie_xi")[1]).suit
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.nullified = true
  end,
})

return sk__huaibi
